package com.jtop.service.ida.spls.common.omiis.onlinecontrol.interceptor;

import com.alibaba.druid.util.StringUtils;
import com.jtop.core.AbstractInterceptor;
import com.jtop.core.Constants;
import com.jtop.core.JTopRequestContext;
import com.jtop.service.bo.spls.security.model.Permission;
import com.jtop.service.ida.spls.common.omiis.onlinecontrol.response.VoiceOnlineControlInterceptorResponse;
import com.jtop.service.util.AuthenticationUtil;
import org.apache.log4j.Logger;

/**
 * This is about <code>com.jtop.service.ida.spls.common.omiis.voice.onlinecontrol.OnlineControlInterceptor</code>.
 * Created by 2017年04月09日 20:36.
 *
 * @author <a href="mailto:xueqismwm@163.com">薛琦</a>
 * @version V1.0
 */
public class VoiceOnlineControlInterceptor extends AbstractInterceptor {

    protected final Logger log = Logger.getLogger(getClass());

    /**
     * 在数据绑定后，服务方法调用前执行该拦截方法
     *
     * @param jTopRequestContext
     */
    public void beforeService(JTopRequestContext jTopRequestContext) {

        log.debug("VoiceOnlineControlInterceptor beforeService ...");
        if(jTopRequestContext.getSession() != null &&
                !StringUtils.equals(jTopRequestContext.getMethod(), "ContactBaseInfoService.saveOrUpdateContactBaseInfo")) {
            Object permissionId = jTopRequestContext.getSession().getAttribute(Constants.CURRENT_PERMISSION_ID);
            if (permissionId != null) {
                Permission permission = (Permission) AuthenticationUtil.getPermission(permissionId.toString());
                if (permission != null && StringUtils.equals(Constants.AVAILDATION_STATUS, permission.getOnlineSign())) {
                    Object onlineSign = jTopRequestContext.getSession().getAttribute(Constants.CURRENT_ONLINE_SIGN);
                    if (onlineSign == null || !Boolean.parseBoolean(onlineSign.toString())) {
                        VoiceOnlineControlInterceptorResponse reponse = new VoiceOnlineControlInterceptorResponse();
                        reponse.setBusinessMessage("请求被拒绝，功能[" + permission.getName() + "]为限制性功能，需电话接通情况下方可操作!", REDIRECT_DEFAULT_ERROR_PAGE);
                        jTopRequestContext.setJTopResponse(reponse);
                    }
                }
            }
        }
    }

    /**
     * 在服务执行完成后，响应返回前执行该拦截方法
     *
     * @param jTopRequestContext
     */

    public void beforeResponse(JTopRequestContext jTopRequestContext) {
        log.debug("VoiceOnlineControlInterceptor beforeResponse ...");

    }

    /**
     * 对method为user.add的方法进行拦截
     *
     * @param jTopRequestContext
     * @return
     */

    public boolean isMatch(JTopRequestContext jTopRequestContext) {

        return true;
    }
}
